<template>
  <div class="container">
    <el-dialog
      title="品类信息"
      :visible.sync="categoryDialog.centerDialogVisible"
      width="70%"
      center
      :lock-scroll="true"
      @open="getData"
      @close="cancel"
    >
      <el-input
        style="width:200px;margin:0 10px"
        v-model="search.value"
        clearable
        placeholder="请输入内容"
      ></el-input>
     
      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        class="handleSearch"
        @click="handleSearch()"
      >查询</el-button>
      <div class="tagList">
        <el-tag v-for="tag in tags" :key="tag.name" closable @close="handleClose(tag)">{{tag.name}}</el-tag>
      </div>
      <el-table
        :data="tableData"
        max-height="450px"
        border
        style="margin-top:10px"
        ref="multipleTable"
        empty-text="暂无数据"
      >
        <el-table-column prop="name" label="品类名称" align="center"></el-table-column>
        <el-table-column prop="code" label="品类编码" align="center"></el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button
              class="add"
              size="small"
              type="primary"
              icon="el-icon-circle-plus"
              @click="add(scope)"
            >添加</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pagination @page="handleSizeChange" @pagesize="handleCurrentChange" :pageData="pageData"></pagination>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import { getProductClassInfoSelect } from "@/api/salesMarginChart/salesMarginChartList";
export default {
  props: {
    categoryDialog: Object,
  },
  components: {
    pagination: Pagination,
  },
  data() {
    return {
      search: {
        value: "",
      },
      tags: [],
      tableData: [],
     
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
    };
  },
  methods: {
    getData() {
      let data =
        "&keyWord=" +
        this.search.value +
        "&pageSize=" +
        this.pageData.pageSize +
        "&currPage=" +
        this.pageData.page;
      getProductClassInfoSelect(data).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.content;
          this.pageData.page = res.data.currentPage;
          this.pageData.total = res.data.total;
        }
      });
    },
    
    //添加
    add(scope) {
      let arr = this.tags.filter((res) => {
        return res.id == scope.row.id;
      });
      if (arr.length > 0) {
        this.$message({
          type: "info",
          message: "该分类已添加",
        });
      } else {
        this.tags.push({
          name: scope.row.name,
          id: scope.row.id,
        });
      }
    },
    //删除
    handleClose(tag) {
      this.tags.splice(this.tags.indexOf(tag), 1);
    },
    hideDialog() {
      this.categoryDialog.centerDialogVisible = false;
    },
    //确定
    confirm() {
      this.$emit("tag", this.tags);
      this.cancel();
    },
    //取消
    cancel() {
      this.search.value = "";
      this.formData = [];
      this.tags = [];
      this.hideDialog();
    },
    //搜索
    handleSearch() {
      this.pageData.total = null;
      this.pageData.page = 1;
      this.getData();
    },
    
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
  },
};
</script>
<style scoped>
.tagList {
  margin-top: 10px;
}
.el-tag {
  margin-right: 5px;
  margin-top: 2px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>